package com.kma.summer2012.dao;

import com.kma.summer2012.factory.HibernateUtil;
import com.kma.summer2012.model.CrumbsRequestAndWork;
import org.hibernate.Session;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Администратор
 * Date: 15.01.13
 * Time: 19:00
 * To change this template use File | Settings | File Templates.
 */
public class CrumbsRequestAndWorkDAOImpl implements DAO<CrumbsRequestAndWork>{
    @Override
    public void add(CrumbsRequestAndWork requestAndWork) {
        Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            session.save(requestAndWork);
            session.getTransaction().commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
    }

    @Override
    public void update(CrumbsRequestAndWork requestAndWork) {
        Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            session.update(requestAndWork);
            session.getTransaction().commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
    }

    @Override
    public CrumbsRequestAndWork getById(int requestAndWork_id) {
        Session session = null;
        CrumbsRequestAndWork requestAndWork = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            requestAndWork = (CrumbsRequestAndWork) session.get(CrumbsRequestAndWork.class, requestAndWork_id);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
        return requestAndWork;
    }

    @Override
    public Collection<CrumbsRequestAndWork> getAll() {
        Session session = null;
        List requestAndWorks = new ArrayList<CrumbsRequestAndWork>();
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            requestAndWorks = session.createCriteria(CrumbsRequestAndWork.class).list();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
        return requestAndWorks;
    }

    @Override
    public void delete(CrumbsRequestAndWork requestAndWork) {
        Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            session.delete(requestAndWork);
            session.getTransaction().commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
    }

}

